package com.ruoyi.base.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.ruoyi.Utils.MaterialsUtil;
import com.ruoyi.Utils.ProbabilityUtil;
import com.ruoyi.base.domain.MaterialsParams;
import com.ruoyi.base.domain.Touch;
import com.ruoyi.base.domain.TouchVo;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.base.mapper.SjzMaterialsMapper;
import com.ruoyi.base.domain.SjzMaterials;
import com.ruoyi.base.service.ISjzMaterialsService;

/**
 * 物品材料Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-01
 */
@Service
public class SjzMaterialsServiceImpl implements ISjzMaterialsService 
{
    @Autowired
    private SjzMaterialsMapper sjzMaterialsMapper;

    /**
     * 查询物品材料
     * 
     * @param id 物品材料主键
     * @return 物品材料
     */
    @Override
    public SjzMaterials selectSjzMaterialsById(Long id)
    {
        return sjzMaterialsMapper.selectSjzMaterialsById(id);
    }

    /**
     * 查询物品材料列表
     * 
     * @param sjzMaterials 物品材料
     * @return 物品材料
     */
    @Override
    public List<SjzMaterials> selectSjzMaterialsList(SjzMaterials sjzMaterials)
    {
        return sjzMaterialsMapper.selectSjzMaterialsList(sjzMaterials);
    }

    /**
     * 新增物品材料
     * 
     * @param sjzMaterials 物品材料
     * @return 结果
     */
    @Override
    public int insertSjzMaterials(SjzMaterials sjzMaterials)
    {
        return sjzMaterialsMapper.insertSjzMaterials(sjzMaterials);
    }

    /**
     * 修改物品材料
     * 
     * @param sjzMaterials 物品材料
     * @return 结果
     */
    @Override
    public int updateSjzMaterials(SjzMaterials sjzMaterials)
    {
        return sjzMaterialsMapper.updateSjzMaterials(sjzMaterials);
    }

    /**
     * 批量删除物品材料
     * 
     * @param ids 需要删除的物品材料主键
     * @return 结果
     */
    @Override
    public int deleteSjzMaterialsByIds(Long[] ids)
    {
        return sjzMaterialsMapper.deleteSjzMaterialsByIds(ids);
    }

    /**
     * 删除物品材料信息
     * 
     * @param id 物品材料主键
     * @return 结果
     */
    @Override
    public int deleteSjzMaterialsById(Long id)
    {
        return sjzMaterialsMapper.deleteSjzMaterialsById(id);
    }

    @Override
    public AjaxResult synSjzMaterials(MaterialsParams materialsParams) {
        List<SjzMaterials> list = MaterialsUtil.getSjzMaterialsNo(materialsParams);
        if (list.isEmpty()){
            return AjaxResult.error("获取数据失败");
        }
        Long[] ids = list.stream().map(SjzMaterials::getId).distinct().toArray(Long[]::new);
        deleteSjzMaterialsByIds(ids);
        for (SjzMaterials sjzMaterials : list){
            insertSjzMaterials(sjzMaterials);
        }
        return AjaxResult.success();
    }

    @Override
    public List<TouchVo> touchMet() {
        List<TouchVo> TouchVo = new ArrayList<>();
        int num = ProbabilityUtil.getRandomNum();
        List<Touch> list = new ArrayList<>();
        int width = 0;
        for (int i = 0; i < num; i++){
            // 物品等级
            int grade = ProbabilityUtil.getRandomItemLevel();
            Touch touch = sjzMaterialsMapper.randTouch(grade);
            width += touch.getWidth();
            if(width>=4)
            {
                break;
            }
            list.add(touch);
        }
        TouchVo.add(new TouchVo(list));
        for (int i=1;i<4;i++){
            TouchVo.add(new TouchVo());
        }
        return TouchVo;
    }
}
